package com.lipengqing.common.utils;

import com.lipengqing.common.utils.uuid.UUID;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * 生成 aes key iv
 * @author lipengqing
 */
public class Aes {

    private String Key;
    private String Iv;
    private static final String CBC_PKCS5_PADDING = "AES/CBC/PKCS5Padding";
    private static final String AES = "AES";
    //构造函数进行初始化key和iv（随机生成）
    public Aes(){
        this.Key = getRandom(16);
        this.Iv = getRandom(16);
    }
    public String getKey(){
        return this.Key;
    }
    public String getIv(){
        return this.Iv;
    }
    public String getRandom(int length){
        char[] arr = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k',
                'l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
        String result = String.valueOf(arr[(int)Math.floor(Math.random()*36)]);
        for(int i = 1;i<length;i++){
            result+=arr[(int)Math.floor(Math.random()*36)];
        }
        return result;
    }
    //进行加密返回一个byte[]
    public byte[] encrypt(String content){
        byte[] result = null;
        byte[] keyCode = Key.getBytes(StandardCharsets.UTF_8);
        Cipher cipher = null;
        try {
            cipher = Cipher.getInstance(CBC_PKCS5_PADDING);
            IvParameterSpec zeroIv = new IvParameterSpec(Iv.getBytes(StandardCharsets.UTF_8));
            SecretKeySpec skeySpec = new SecretKeySpec(keyCode,AES);
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, zeroIv);// 初始化为加密模式的密码器
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
        byte[] byteContent = content.getBytes(StandardCharsets.UTF_8);
        try {
            result = cipher.doFinal(byteContent);// 加密
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        return result;
    }
    //进行解密返回一个String
    public static final String decrypt(byte[] bytes,String key,String iv){
        String result = null;
        byte[] byteIv = iv.getBytes(StandardCharsets.UTF_8);
        byte[] byteKey = key.getBytes(StandardCharsets.UTF_8);
        IvParameterSpec myIv = new IvParameterSpec(byteIv);
        SecretKeySpec myKey = new SecretKeySpec(byteKey,AES);
        Cipher cipher = null;
        try {
            cipher = Cipher.getInstance(CBC_PKCS5_PADDING);
            cipher.init(Cipher.DECRYPT_MODE, myKey, myIv);// 初始化
            byte[] result1 = cipher.doFinal(bytes);
            result = new String(result1,0,result1.length);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void main(String[] args) {
        UUID uuid = UUID.randomUUID();
        System.out.println(uuid);
        System.out.println(uuid.toString().replaceAll("-", ""));
        Aes aes = new Aes();
        System.out.println(aes.getKey());
        System.out.println(aes.getIv());

        byte[] miwen = aes.encrypt("测试");
        System.out.println(miwen);
        String decrypt = Aes.decrypt(miwen, aes.Key, aes.Iv);
        System.out.println(decrypt);
    }

}
